//
//  AccountController.swift
//  LiteAccount
//
//  Created by 刘伟 on 2017/5/19.
//  Copyright © 2017年 刘伟. All rights reserved.
//

import UIKit

class AccountController: UIViewController, BaseViewProtocal {

    var data: AccountModel! {
        didSet {
            collectionView.reloadData()
            self.navigationItem.title = data.date
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setBaseViewConfig()
        prepareUI()
        layoutUI()
    }
    
    fileprivate func prepareUI() {
        edgesForExtendedLayout = .init(rawValue: 0)
        self.view.backgroundColor = .white
        self.view.addSubview(collectionView)
    }
    
    fileprivate func layoutUI() {
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        collectionView.register(BookAccountCell.self, forCellWithReuseIdentifier: "Cell")
        collectionView.register(HomeHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerView")
    }
    
    // collectionView 的布局
    var layout: UICollectionViewFlowLayout {
        let object = UICollectionViewFlowLayout()
        // 设置 滑动方向
        object.scrollDirection = .vertical
        // 设置 每个 item 的大小
        object.itemSize = CGSize(width: (SCREEN_WIDTH - 15) / 2, height: 100)
        // 设置 每个 section 上下左右的间距
        object.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
        // 设置 行内 item 的最小间隔
        object.minimumInteritemSpacing = 5
        // 设置 每行 cell 之间的最小距离
        object.minimumLineSpacing = 5;
        // 设置 section 头部尺寸
        object.headerReferenceSize = CGSize(width: SCREEN_WIDTH, height: 120)
        return object
    }
    
    lazy var collectionView: UICollectionView = {
        let object = UICollectionView(frame: CGRect.zero, collectionViewLayout: self.layout)
        object.backgroundColor = UIColor.white
        object.bounces = true
        object.bouncesZoom = true
        object.delaysContentTouches = true
        object.contentMode = .scaleToFill
        object.dataSource = self
        object.delegate = self
        return object
    }()
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

extension AccountController: UICollectionViewDelegate, UICollectionViewDataSource {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerView", for: indexPath) as! HomeHeaderView
        headerView.data = self.data
        return headerView
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data.detail.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! BookAccountCell
        cell.type = .edit
        cell.data = self.data.detail[indexPath.row]
        return cell
    }
}
